﻿
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

using System.Web.UI.WebControls;

namespace QuanLyVanBanBUS
{
    public class NoiNhanBUS
    {
        public void themNoiNhan(NoiNhan nnDTO)
        {
            QLVBDataContext storeDB = new QLVBDataContext();

            storeDB.NoiNhans.InsertOnSubmit(nnDTO);

            storeDB.SubmitChanges();
        }

        public void xoaNoiNhanTheoMaNguoiDung(NoiNhan nnDTO, string maNguoiDung, string maVanBan)
        {
            QLVBDataContext storeDB = new QLVBDataContext();

            var query = storeDB.NoiNhans.Where(q => q.MaNguoiNhan == maNguoiDung && q.MaVanBan == maVanBan).First();

            query.BiXoa = nnDTO.BiXoa;

            storeDB.SubmitChanges();
        }

        public Boolean kiemtraTonTaiMaNoiNhan()
        {
            QLVBDataContext storeDB = new QLVBDataContext();

            var query = storeDB.NoiNhans.Select(q => q.MaNoiNhan).ToList();

            if (query.Count > 0)
            {
                return true;
            }

            return false;
        }

        public Boolean kiemtraNamHienTai()
        {
            QLVBDataContext storeDB = new QLVBDataContext();

            int namHienTai = DateTime.Now.Date.Year;

            int namMax = Convert.ToInt32(storeDB.NoiNhans.Select(m => m.MaNoiNhan).Max().Substring(0, 4));

            if (namHienTai >= namMax)
            {
                return true;
            }

            return false;
        }

        public string layMaNoiNhanMax()
        {
            QLVBDataContext storeDB = new QLVBDataContext();

            if (kiemtraTonTaiMaNoiNhan() == true)
            {
                if (kiemtraNamHienTai() == true)
                {
                    string maNoiNhanMax = storeDB.NoiNhans.Select(m => m.MaNoiNhan).Max().Substring(4, 6);

                    return maNoiNhanMax;
                }
            }

            return null;
        }

        public Boolean kiemtraTonTaiMaVanBanTheoMaNguoiNhan(string maVanBan, string maNguoiNhan)
        {
            QLVBDataContext storeDB = new QLVBDataContext();

            var query = storeDB.NoiNhans.Where(q => q.MaVanBan == maVanBan && q.MaNguoiNhan == maNguoiNhan).ToList();

            if (query.Count > 0)
            {
                return true;
            }

            return false;
        }

        public Boolean kiemtraTrangThaiBiXoaTheoMaNguoiNhan(string maVanBan, string maNguoiNhan)
        {
            QLVBDataContext storeDB = new QLVBDataContext();

            var query = storeDB.NoiNhans.Where(q => q.MaVanBan == maVanBan && q.MaNguoiNhan == maNguoiNhan).Select(b => b.BiXoa).First();

            if (query == 1)
            {
                return true;
            }

            return false;
        }

        public Boolean kiemtraTonTaiMaVanBanTableNoiNhan(string maVanBan)
        {
            QLVBDataContext storeDB = new QLVBDataContext();

            var query = storeDB.NoiNhans.Where(q => q.MaVanBan == maVanBan).ToList();

            if (query.Count > 0)
            {
                return true;
            }

            return false;
        }

        public List<NoiNhan> layDanhSachMaNguoiNhanTheoMaVanBanChuaDuyet(string maVanBan)
        {
            List<NoiNhan> lstNoiNhan = new List<NoiNhan>();
            QLVBDataContext storeDB = new QLVBDataContext();
            return storeDB.NoiNhans.Where(q => q.MaVanBan == maVanBan && q.KiemSoat == 0 && q.BiXoa == 0).ToList();
        }

        public List<NoiNhan> layDanhSachMaNguoiNhanTheoMaVanBanDaDuyet(string maVanBan)
        {
            List<NoiNhan> lstNoiNhan = new List<NoiNhan>();
            QLVBDataContext storeDB = new QLVBDataContext();
            return storeDB.NoiNhans.Where(q => q.MaVanBan == maVanBan && q.KiemSoat == 1 && q.BiXoa == 0).ToList();
        }

        public Boolean kiemtraTonTaiMaNguoiDungLanhDaoPhongNhanVanBan(NoiNhan nnDTO, string maNguoiDung, string maVanBan)
        {
            QLVBDataContext storeDB = new QLVBDataContext();

            var query = storeDB.NoiNhans.Where(q => q.BiXoa == 0 && q.KiemSoat == 0 && q.MaVanBan == maVanBan && q.MaNguoiNhan == maNguoiDung).Select(q => q.MaNguoiNhan).ToList();

            if (query.Count > 0)
            {
                return true;
            }

            return false;
        }

        public IList<NoiNhan> layDanhSachNguoiDungNhanVanBanTrongNgay()
        {
            QLVBDataContext storeDB = new QLVBDataContext();

            var query = storeDB.NoiNhans.Where(q => q.NgayNhan.Value.Date == DateTime.Today.Date && q.KiemSoat == 1 && q.BiXoa == 0).Select(q => q.MaNguoiNhan).Distinct().ToList();

            List<NoiNhan> lstMaNguoiNhan = new List<NoiNhan>();

            foreach (String maNguoiNhan in query)
            {
                NoiNhan nnDTO = new NoiNhan();
                nnDTO.MaNguoiNhan = maNguoiNhan;
                lstMaNguoiNhan.Add(nnDTO);
            }

            return lstMaNguoiNhan;
        }

        public String layNgayNhanGanNhatTheoMaNguoiDung(NoiNhan nnDTO)
        {
            QLVBDataContext storeDB = new QLVBDataContext();

            var query = storeDB.NoiNhans.Where(q => q.NgayNhan.Value.Date == DateTime.Today.Date && q.KiemSoat == 1 && q.BiXoa == 0 && q.MaNguoiNhan == nnDTO.MaNguoiNhan).Select(q => q.NgayNhan.Value.TimeOfDay).Max().ToString();

            return query;
        }

        public void xoaNoiNhanTheoMaVanBan(NoiNhan nnDTO)
        {
            QLVBDataContext storeDB = new QLVBDataContext();

            var query = storeDB.NoiNhans.Where(q => q.MaVanBan == nnDTO.MaVanBan && q.KiemSoat == 0 && q.BiXoa == 0).ToList();
            foreach (NoiNhan nn in query)
            {
                storeDB.NoiNhans.DeleteOnSubmit(nn);
            }

            storeDB.SubmitChanges();
        }

        public void capnhatNgayXemVanBan(NoiNhan nnDTO)
        {
            QLVBDataContext storeDB = new QLVBDataContext();

            if (nnDTO.MaNguoiNhan != null && nnDTO.MaVanBan != null)
            {
                var query = storeDB.NoiNhans.Where(q => q.MaNguoiNhan == nnDTO.MaNguoiNhan && q.MaVanBan == nnDTO.MaVanBan).First();
                query.NgayXem = DateTime.Now;

                storeDB.SubmitChanges();
            }
        }
    }
}
